// releasePage/components/nfcLineContent/nfcLineContent.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        nfcLineName: String,
        nfcLineDiscription: String,
        deviceSupport: String,
        editId: String,
        nfcCompany: String,
        nfcCompanyId: String,
    },
    pageLifetimes: {
        show: function () {
            // 页面被展示
            if (
                this.data.deviceSupport &&
                this.data.nfcLineName &&
                this.data.nfcLineDiscription &&
                this.data.sceneImageArrOne.length &&
                this.data.nfcCompany
            ) {
                this.triggerEvent('btnEvent', 3); //通过triggerEvent将参数传给父组件
            } else {
                this.triggerEvent('btnEvent', 100);
            }
            if (this.data.editId && this.data.edit) {
                this.editContent();
            }
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
        edit: true,
        city: '',
        country: '',
        sceneImageArrOne: [], //上传照片
        sceneImageArr: [], //上传照片
        nfcLineDescription: '',
        nfcLineDescriptionLength: 0,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        nfcLineNameTap(e) {
            wx.navigateTo({
                url: `../../pages/homePageInfo/addressName/addressName?current=3&nfcAddressName=${this.data.nfcLineName}`,
            });
        },
        //描述页面跳转
        discriptionTap(e) {
            wx.navigateTo({
                url: `../../pages/homePageInfo/description/description?current=3&description=${this.data.nfcLineDiscription}`,
            });
        },
        //设备支持
        deviceSupportTap(e) {
            wx.navigateTo({
                url: `../../pages/homePageInfo/deviceSupport/deviceSupport`,
            });
        },
        nfcLineDescription(e) {
            this.setData({
                nfcLineDescription: e.detail.value,
                nfcLineDescriptionLength: e.detail.value.length,
            });
        },
        nfcbrushCardTap(e) {
            wx.navigateTo({
                url: `../../pages/homePageInfo/brashCard/brashCard?current=3&nfcbrushCardName=${this.data.nfcCompany}`,
            });
        },
        //再次编辑
        editContent() {
            let _that = this;
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/getNfcTestLineDetail?authId=${app.globalData.authId}&id=${_that.data.editId}`,
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                },
                success: function (res) {
                    wx.setNavigationBarTitle({
                        title: '编辑/' + res.data.nfcTestLine.city,
                    });
                    let support = '';
                    if (res.data.nfcTestLine.nfcflashpay == '00') {
                        support = support + '/NFC云闪付';
                    }
                    if (res.data.nfcTestLine.nfcwallet == '00') {
                        support = support + '/NFC钱包';
                    }
                    if (res.data.nfcTestLine.handheld == '00') {
                        support = support + '/手持设备';
                    }
                    if (res.data.nfcTestLine.bomtest == '00') {
                        support = support + '/BOM测试';
                    }
                    if (res.data.nfcTestLine.selfcheckticket == '00') {
                        support = support + '/自助查票机';
                    }
                    _that.setData({
                        nfcLineName: res.data.nfcTestLine.linename,
                        nfcLineDiscription: res.data.nfcTestLine.description,
                        deviceSupport: support.slice(1),
                        sceneImageArrOne: JSON.parse(
                            res.data.nfcTestLine.picurl
                        ), //上传照片
                        edit: false,
                        nfcCompany: res.data.nfcTestLine.devicemanufacturer,
                        nfcCompanyId: res.data.nfcTestLine.devicemanufacturerid,
                        city: res.data.nfcTestLine.city,
                        country: res.data.nfcTestLine.country,
                    });
                },
            });
        },
        // textarea聚焦隐藏提交按钮
        focusHideBtn() {
            this.triggerEvent('focusHideBtn');
        },
        // textarea失焦显示提交按钮
        blurShowBtn() {
            this.triggerEvent('blurShowBtn');
        },
        // 添加图片顶部
        addImgOne(e) {
            const that = this;
            wx.chooseImage({
                count: 8 - that.data.sceneImageArrOne.length,
                success(res) {
                    let imgLen =
                        that.data.sceneImageArrOne.length +
                        res.tempFilePaths.length;
                    if (imgLen > 8) {
                        wx.showModal({
                            content: '最多上传8张图片，请重新上传',
                            showCancel: false,
                        });
                        return;
                    } else {
                        wx.showLoading({
                            title: '图片上传中',
                            mask: true,
                        });
                        const imgUploadResults = [];
                        let index = that.data.sceneImageArrOne.length;
                        for (let i = 0; i < res.tempFilePaths.length; i++) {
                            let imgUploadResult = app
                                .imgUpload(res.tempFilePaths[i])
                                .then((res) => {
                                    let num = i + index;
                                    that.data.sceneImageArrOne[num] = res;
                                    that.setData({
                                        sceneImageArrOne:
                                            that.data.sceneImageArrOne,
                                    });
                                    console.log(that.data.sceneImageArrOne);
                                });
                            imgUploadResults.push(imgUploadResult);
                        }
                        Promise.all(imgUploadResults).then((values) => {
                            wx.hideLoading();
                            if (
                                that.data.deviceSupport &&
                                that.data.nfcLineName &&
                                that.data.nfcLineDiscription &&
                                values.length
                            ) {
                                that.triggerEvent('btnEvent', 3); //通过triggerEvent将参数传给父组件
                            }
                        });
                    }
                },
            });
        },
        // 删除图片
        deleteImgOne(e) {
            let index = e.currentTarget.dataset.index;
            console.log(index);
            this.data.sceneImageArrOne.splice(index, 1);
            this.setData({
                sceneImageArrOne: this.data.sceneImageArrOne,
            });
            if (
                this.data.deviceSupport &&
                this.data.nfcLineName &&
                this.data.nfcLineDiscription &&
                this.data.sceneImageArrOne.length &&
                this.data.nfcCompany
            ) {
                this.triggerEvent('btnEvent', 3); //通过triggerEvent将参数传给父组件
            } else {
                this.triggerEvent('btnEvent', '');
            }
        },
        // 添加图片
        addImg(e) {
            const that = this;
            wx.chooseImage({
                count: 8 - that.data.sceneImageArr.length,
                success(res) {
                    let imgLen =
                        that.data.sceneImageArr.length +
                        res.tempFilePaths.length;
                    if (imgLen > 8) {
                        wx.showModal({
                            content: '最多上传8张图片，请重新上传',
                            showCancel: false,
                        });
                        return;
                    } else {
                        wx.showLoading({
                            title: '图片上传中',
                            mask: true,
                        });
                        const imgUploadResults = [];
                        let index = that.data.sceneImageArr.length;
                        for (let i = 0; i < res.tempFilePaths.length; i++) {
                            let imgUploadResult = app
                                .imgUpload(res.tempFilePaths[i])
                                .then((res) => {
                                    let num = i + index;
                                    let imgArr = 'sceneImageArr[' + num + ']';
                                    that.setData({
                                        [imgArr]: res,
                                    });
                                    console.log(res);
                                });
                            imgUploadResults.push(imgUploadResult);
                        }
                        Promise.all(imgUploadResults).then((values) => {
                            wx.hideLoading();
                        });
                    }
                },
            });
        },
        // 删除图片
        deleteImg(e) {
            let index = e.currentTarget.dataset.index;
            console.log(index);
            this.data.sceneImageArr.splice(index, 1);
            this.setData({
                sceneImageArr: this.data.sceneImageArr,
            });
        },
        // 预览图片
        previewImage(e) {
            const current = e.target.dataset.src;
            const currentArr = e.target.dataset.srcarr;
            wx.previewImage({
                current,
                urls: currentArr,
            });
        },
        // 提交
        submit(review) {
            let _that = this;
            if (_that.data.editId) {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/editNfcTestLine?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        id: _that.data.editId,
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        effectiveflg: '00',
                        picurl: JSON.stringify(_that.data.sceneImageArrOne),
                        linename: _that.data.nfcLineName, //线路名称
                        description: _that.data.nfcLineDiscription, //线路描述
                        nfcflashpay:
                            _that.data.deviceSupport.indexOf('NFC云闪付') == -1
                                ? '01'
                                : '00', //是否支持NFC闪付 00是 01否
                        bomtest:
                            _that.data.deviceSupport.indexOf('BOM测试') == -1
                                ? '01'
                                : '00', //是否支持BOM测试 00是 01否
                        selfcheckticket:
                            _that.data.deviceSupport.indexOf('自助查票机') == -1
                                ? '01'
                                : '00', //是否有自助查票机 00是 01否
                        nfcwallet:
                            _that.data.deviceSupport.indexOf('NFC钱包') == -1
                                ? '01'
                                : '00', //是否支持NFC钱包 00是 01否
                        handheld:
                            _that.data.deviceSupport.indexOf('手持设备') == -1
                                ? '01'
                                : '00', //是否手持设备 00是 01否
                        city: _that.data.city || app.globalData.addressBack, //城市
                        country:
                            _that.data.country || app.globalData.countryBack, //国家
                        devicemanufacturerid: _that.data.nfcCompanyId,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            if (review) {
                                app.doVerify(res.data.data.id, '04');
                            } else {
                                let pages = getCurrentPages(); //当前页面栈
                                let prevPage = pages[pages.length - 2]; //上一页面
                                prevPage.setData({
                                    //本页面数据赋值到上一个页面
                                    id: res.data.data.id,
                                });
                            }
                            wx.showToast({
                                title: '提交成功',
                                icon: 'success',
                                duration: 2000,
                            });
                        } else {
                            wx.showToast({
                                title: '提交失败，请重新录入或联系后台管理',
                                icon: 'none',
                                duration: 2000,
                            });
                        }
                    },
                });
            } else {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/createNfcTestLine?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        effectiveflg: '00',
                        picurl: JSON.stringify(_that.data.sceneImageArrOne),
                        linename: _that.data.nfcLineName, //线路名称
                        description: _that.data.nfcLineDiscription, //线路描述
                        nfcflashpay:
                            _that.data.deviceSupport.indexOf('NFC云闪付') == -1
                                ? '01'
                                : '00', //是否支持NFC闪付 00是 01否
                        bomtest:
                            _that.data.deviceSupport.indexOf('BOM测试') == -1
                                ? '01'
                                : '00', //是否支持BOM测试 00是 01否
                        selfcheckticket:
                            _that.data.deviceSupport.indexOf('自助查票机') == -1
                                ? '01'
                                : '00', //是否有自助查票机 00是 01否
                        nfcwallet:
                            _that.data.deviceSupport.indexOf('NFC钱包') == -1
                                ? '01'
                                : '00', //是否支持NFC钱包 00是 01否
                        handheld:
                            _that.data.deviceSupport.indexOf('手持设备') == -1
                                ? '01'
                                : '00', //是否手持设备 00是 01否
                        excontent: _that.data.nfcLineDescription, //经验内容
                        expicurls: _that.data.sceneImageArr, //经验图片
                        city: app.globalData.addressBack, //城市
                        country: app.globalData.countryBack, //国家
                        devicemanufacturerid: _that.data.nfcCompanyId,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            wx.showToast({
                                title: '提交成功',
                                icon: 'success',
                                duration: 2000,
                            });
                        } else {
                            wx.showToast({
                                title: '提交失败，请重新录入或联系后台管理',
                                icon: 'none',
                                duration: 2000,
                            });
                        }
                    },
                });
            }
        },
    },
});
